package com.dhms.vostok1.service.net

import com.dhms.vostok1.data.*
import com.dhms.vostok1.data.spot_patrol.PlanType
import com.dhms.vostok1.data.spot_patrol.TaskStatus
import com.dhms.vostok1.network.BaseNetworkApi
import com.dhms.vostok1.network.MoshiObject
import retrofit2.Converter
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.Body
import retrofit2.http.Path
import retrofit2.http.Query

object MockNetworkAPI : BaseNetworkApi<IMockNetworkAPI>() {
    /**
     * offset: 当前页码
     * limit: 单页条数
     * query_task_type: 查询任务类型 null: 全部 , PlanType
     * query_task_status: 查询任务状态 null: 全部, TaskStatus
     */
    suspend fun getTaskGroups(
        @Query("offset") offset: Int,
        @Query("limit") limit: Int = 10,
        @Query("query_task_type") query_task_type: List<PlanType>? = null,
        @Query("query_task_status") query_task_status: List<TaskStatus>? = null,
    ) = getResult {
        val taskType = query_task_type?.map { it.code }.toString()
        val taskStatus = query_task_status?.map { it.code }.toString()

        service.getTaskGroups(offset, limit, taskType, taskStatus)
    }

    /*查询任务详情*/
    suspend fun getTaskDetail(
        @Path("group_id") group_id: String,
        @Path("task_id") task_id: String
    ) = getResult {
        service.getTaskDetail(group_id, task_id)
    }

    /*更新任务*/
    suspend fun updateTask(@Body body: TaskUpdateReqBody) = getResult {
        service.updateTask(body)
    }

    /*批量更新任务组中的执行人*/
    suspend fun updateGroupExecutors(
        @Path("group_id") group_id: String,
        @Body body: TaskExecutorsReqBody
    ) = getResult {
        service.updateGroupExecutors(group_id, body)
    }

    /*查询任务组详情*/
    suspend fun getTaskGroupDetail(@Path("group_id") group_id: String) = getResult {
        service.getTaskGroupDetail(group_id)
    }


    /*异常记录条目*/
    suspend fun getAbnormals(body: AbnormalsReqBody) = getResult {
        service.getAbnormals(
            body.page,
            body.per_page,
            body.start_time,
            body.end_time,
            body.device_ids,
            body.status,
            body.source
        )
    }

    /*异常上报*/
    suspend fun createAbnormals(@Body body: AbnormalsCreateReqBody) = getResult {
        service.createAbnormals(body)
    }

    /*异常记录详情*/
    suspend fun getAbnormalsDetail(@Path("id") id: String) = getResult {
        service.getAbnormalsDetail(id)
    }

    /*更新异常处理*/
    suspend fun updateAbnormalsDetail(
        @Path("id") id: String,
        @Body body: AbnormalsUpdateReqBody
    ) = getResult {
        service.updateAbnormalsDetail(id, body)
    }

    /*点检异常查询*/
    suspend fun getAbnormalsItems(
        @Query("page") page: Int,
        @Query("per_page") per_page: Int = 40,
    ) = getResult {
        service.getAbnormalsItems(page, per_page)
    }

    /*点检异常项添加*/
    suspend fun createAbnormalsItems(
        @Body body: AbnormalItemsReqBody
    ) = getResult {
        service.createAbnormalsItems(body)
    }

    /*点检异常项删除*/
    suspend fun deleteAbnormalsItems(
        @Path("id") id: String,
    ) = getResult {
        service.deleteAbnormalsItems(id)
    }

    override fun initFactory(): Converter.Factory = MoshiConverterFactory.create(MoshiObject.moshi)

}